package com.koron.car.web.driver.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.car.config.Constants;
import com.koron.car.web.driver.bean.query.DriverQuery;
import com.koron.car.web.driver.bean.vo.DriverVO;
import com.koron.car.web.driver.filter.DriverFilter;
import com.koron.car.web.driver.service.DriverService;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Objects;

@Slf4j
@Api(tags = "车辆管理-司机台账")
@RestController
@RequestMapping("/driver")
public class DriverController implements WorkflowController {

    @Autowired
    private DriverService driverService;

    @Autowired
    private DriverFilter filter;

    @GetMapping("/selectOne")
    @ApiOperation("根据司机id查询司机信息")
    @ApiImplicitParam(name = "driverId", value = "司机id", required = true)
    public Response<DriverVO> selectOne(String driverId) {
        log.info("根据司机id查询司机信息， driverId = {}", driverId);
        DriverVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "queryById", DriverVO.class, driverId);
        Assert.isTrue(!Objects.isNull(bean) && !bean.getDeleteFlag(), "司机信息不存在");
        return Response.ok(bean);
    }

    @GetMapping("/fullTime/queryList")
    @ApiOperation("根据条件查询专职司机列表")
    public Response<PageInfo<DriverVO>> queryFullTimeList(DriverQuery query) {
        log.info("根据条件查询专职司机列表， car = {}", JSON.toJSONString(query));
        OrderByInterceptor.setOrderBy(query.getOrders());
        if(CollectionUtils.isEmpty(query.getWorkStates())) {
            query.setWorkStates(Constants.DriverWorkState.stateList);
        }
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<DriverVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "queryList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @GetMapping("/schedulable")
    @ApiOperation("可调度的司机查询")
    public Response<List<DriverVO>> schedulable(DriverQuery query) {
        log.info("可调度的司机查询， query = {}", JSON.toJSONString(query));
        if(CollectionUtils.isEmpty(query.getWorkStates())) {
            query.setWorkStates(Constants.DriverWorkState.stateList);
        }
        List<DriverVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "schedulable", List.class, query);
        return Response.ok(beans);
    }

    @GetMapping("/partTime/queryList")
    @ApiOperation("根据条件查询兼职司机列表")
    public Response<PageInfo<DriverVO>> queryPartTimeList(DriverQuery query) {
        log.info("根据条件查询兼职司机列表， car = {}", JSON.toJSONString(query));
        OrderByInterceptor.setOrderBy(query.getOrders());
        // 只查兼职司机的
        query.setDriverType(Constants.DriverType.PART_TIME);
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<DriverVO> beans = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "queryList", List.class, query);
        return Response.ok(new PageInfo<>(beans));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存和更新司机信息")
    public Response<DriverVO> saveOrUpdate(@RequestBody DriverVO driver) {
        log.info("保存和更新司机信息， driver = {}", JSON.toJSONString(driver));
        // 防止通过保存接口删除数据
        driver.setDeleteFlag(false);
        DriverVO bean = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "saveOrUpdate", DriverVO.class, driver);
        return Response.ok(bean);
    }


    @GetMapping("/updateWorkState")
    @ApiOperation("更新车辆状态")
    public Response<DriverVO> updateWorkState(DriverVO driver) {
        log.info("更新司机工作状态, info = {}", JSON.toJSONString(driver));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "updateWorkState", Boolean.class, driver.getId(), driver.getWorkState());
        return Response.ok(driver);
    }


    @GetMapping("/deleteById")
    @ApiOperation("根据司机id逻辑删除司机信息")
    @ApiImplicitParam(name = "driverId", value = "司机id", required = true)
    public Response<String> deleteById(@CasUser @ApiIgnore EamUser user, @RequestParam String driverId) {
        ADOConnection.runTask(user.getCurrDs(), driverService, "hiddenById", Boolean.class, driverId);
        return Response.ok("删除成功", driverId);
    }

    @GetMapping("/getDriverByPhone")
    @ApiOperation("根据手机号码查询司机信息")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true)
    public Response<DriverVO> getDriverByAccount(@CasUser @ApiIgnore EamUser user, @RequestParam String phone) {
        DriverVO driverVO = ADOConnection.runTask(user.getCurrDs(), driverService, "getDriverByPhone", DriverVO.class, phone);
        return Response.ok(driverVO);
    }

    @Override
    @ApiOperation("修改业务状态, 非前端接口")
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流更新： {}", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    @Override
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工作流完结 ： {}", JSON.toJSONString(redirectQuery));
        DriverVO bean = JSON.parseObject(redirectQuery.getFormVariables(), DriverVO.class);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), driverService, "auditFinish", DriverVO.class, bean);
        return Response.success();
    }

    @Override
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        log.info("updateCancel = {}", JSON.toJSONString(redirectQuery));
        return Response.success();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("兼职司机审批设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     *
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("兼职司机审批选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }

}
